<template>
  <div class="container">
    <div class="login-box">
      <!-- 头像 -->
      <img
        src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573798752336&di=f7043cccf48faccafde3bd87dae575a5&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201508%2F13%2F20150813195230_z3SvH.jpeg"
      />
      <!-- 表单区域 -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="0"
        class="form demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            v-model="ruleForm.username"
            placeholder="请输入用户名"
            prefix-icon="iconfont icon-xingmingyonghuming"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            placeholder="请输入密码"
            prefix-icon="iconfont icon-mima"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      ruleForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    login() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          let { data: res } = await this.$http.post('login', this.ruleForm)
          if (res.meta.status !== 200) {
            this.$message.error(res.meta.msg)
          } else {
            window.sessionStorage.setItem('token', res.data.token)
            this.$message.success('登录成功')
            this.$router.push('/home')
          }
        } else {
          this.$message.error('请填写完整的信息')
        }
      })
    },
    reset() {
      this.$refs.ruleForm.resetFields()
    }
  }
}
</script>
<style lang="less" scoped>
.container {
  background-color: #2b4b6b;
  height: 100%;

  .login-box {
    width: 560px;
    height: 300px;
    border-radius: 15px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    img {
      border-radius: 50%;
      width: 120px;
      height: 120px;
      position: absolute;
      left: 50%;
      top: 0;
      transform: translate(-50%, -50%);
      box-shadow: 0 0 10px 2px rgba(0, 0, 0, 0.3);
    }
    .form {
      margin-top: 80px;
      padding: 20px;
      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
